package com.woke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CjPrize {
	private int id;
	private String cjid;
	private String prizename;
	private String probability;
	private String rank;
	
	
	public String getRank() {
		return rank;
	}
	public void setRank(String rank) {
		this.rank = rank;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	
	public String getCjid() {
		return cjid;
	}
	public void setCjid(String cjid) {
		this.cjid = cjid;
	}
	public String getPrizename() {
		return prizename;
	}
	public void setPrizename(String prizename) {
		this.prizename = prizename;
	}
	public String getProbability() {
		return probability;
	}
	public void setProbability(String probability) {
		this.probability = probability;
	}
	public CjPrize() {

	}
	
	public CjPrize(int id,String cjid, String prizename,String probability,String rank) {
		super();
		this.id = id;
		this.cjid = cjid;
		this.prizename = prizename;
		this.probability=probability;
		this.rank=rank;
	}

	public static List<CjPrize> getData(String sql) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		List<CjPrize> list = new ArrayList<CjPrize>();
		try {
			
			con = WokeConnection.getConnection();
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String cjid = rs.getString("cjid");
				String prizename = rs.getString("prizename");
				String probability = rs.getString("probability");
				String rank = rs.getString("rank");
				CjPrize u = new CjPrize(id, cjid, prizename, probability,rank);
				list.add(u);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			WokeConnection.closeMySql(con, st, rs);
		}
		return list;
	}
	
	public int selectLendSize(){
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		try {
			 pst = conn.prepareStatement("select count(*) from cjprize");
			 rs = pst.executeQuery();
			if(rs.next()){
				int pagecount = rs.getInt(1);
				return pagecount;
			}
			return 0;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return 0;

		}finally{
			WokeConnection.closeMySql(conn, pst, rs);
		}
	}
	
	public List<CjPrize> selectLend(int pageNow, int pageSize){

		List<CjPrize> list = new ArrayList<CjPrize>();
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			int startIndex =0;
			int endIndex = pageSize;
			String sql = "select  * from cjprize ";
			String sw = " where " + " 1=1 " + " order by id Desc ";
			// sql +=" where " + strWhere ;
			if (pageNow > 1) {
				startIndex = pageSize * (pageNow - 1);
				// endIndex = pageSize * currentPage;
			}
			sql += sw + " limit " + startIndex + "," + endIndex;
			
			 pst= conn.prepareStatement(sql);
			 rs= pst.executeQuery();
			while(rs.next()){
				CjPrize s= new CjPrize();
				s.setId(rs.getInt(1));
				s.setCjid(rs.getString(2));
				s.setPrizename(rs.getString(3));
				s.setProbability(rs.getString(4));
				s.setRank(rs.getString(5));
				list.add(s);
			}
			return list;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return null;

		}finally{

			WokeConnection.closeMySql(conn, pst, rs);

		}

	}

	
}
